package com.example.demo.java8.stream;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.IntStream;
import java.util.stream.Stream;

/**
 * 创建流
 *
 * @author Roger
 * @since 2021/7/25
 */
public class CreateStreamTest {
    public static void main(String[] args) {


    //1、通过 java.util.Collection.stream() 方法用集合创建流
    List<String> listStream = Arrays.asList("a", "b", "c");
    // 创建一个顺序流
    Stream<String> stream = listStream.stream();
    // 创建一个并行流
    Stream<String> parallelStream = listStream.parallelStream();
    //2、使用java.util.Arrays.stream(T[] array)方法用数组创建流
    int[] array={1,3,5,6,8};
    IntStream stream2 = Arrays.stream(array);

    // 3、使用Stream的静态方法：of()、iterate()、generate()
    Stream<Integer> stream3 = Stream.of(1, 2, 3, 4, 5, 6);
    Stream<Integer> stream4 = Stream.iterate(0, (x) -> x + 3).limit(4);
    stream4.forEach(System.out::println); // 0 2 4 6 8 10
    Stream<Double> stream5 = Stream.generate(Math::random).limit(3);
        stream5.forEach(System.out::println);
    }
}
